package hot;

/**
 * @author way
 * @create 2025-03-13 22:23
 */
public class _437_pathSum {
    int count;
    int target;

    public int pathSum(TreeNode root, int targetSum) {
        if (root == null) return 0;
        this.target = targetSum;
        pre(root);
        return count;
    }

    private void pre(TreeNode root) {
        if (root == null) return;
        //以该节点开始遍历
        dfs(root, target - root.val);
        pre(root.left);
        pre(root.right);
    }

    private void dfs(TreeNode root, long sum) {
        if (sum == 0) count++;

        if (root.left != null) dfs(root.left, sum - root.left.val);
        if (root.right != null) dfs(root.right, sum - root.right.val);
    }
}
